public class SingleLinkedList {
    //定义一个内部类充当节点
    public class Node {
        public int val;
        public Node next;

        public Node(int val) {
            this.val = val;
        }
    }

    //定义头结点
    public Node head;

    //头插法
    public void addFirst(int data) {
        Node newNode = new Node(data);
        if (head == null) {
            head = newNode;
            return;
        }
        newNode.next = head;
        head = newNode;
    }

    //尾插法
    public void addLast(int data) {
        Node newNode = new Node(data);
        if (head == null) {
            head = newNode;
            return;
        }
        Node cur = head;
        while (cur.next != null) {
            cur = cur.next;
        }
        cur.next = newNode;
    }

    //任意位置插入,第一个数据节点为0号下标
    public boolean addIndex(int index,int data) {
        if (isExit(index)) {
            //要考虑是否为头插
            if (index == 0) {
                addFirst(data);
                return true;
            }
            Node prev = null;
            Node cur = head;
            for (int i = 0; i < index; i++) {
                prev = cur;
                cur = cur.next;
            }
            Node newNode = new Node(data);
            newNode.next = cur;
            prev.next = newNode;
            return true;

        } else {
            return false;
        }
    }

    private boolean isExit(int index) {
        if (index < 0 || index >= size()) {
            return false;
        }
        return true;
    }

    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key) {
        Node cur = head;
        while (cur != null) {
            if (cur.val == key) {
                return true;
            }
            cur = cur.next;
        }

        return false;
    }

    //删除第一次出现关键字为key的节点
    public void remove(int key) {
        Node prev = null;
        Node cur = head;
        while (cur != null) {
            if (cur.val == key) {
                break;
            }
            prev = cur;
            cur = cur.next;
        }

        //注意头删
        if (cur == head) {
            head = cur.next;
            return;
        }

        prev.next = cur.next;
    }
    //删除所有值为key的节点
    public void removeAllKey(int key) {
        //直接使用前后指针法
        Node prev = null;
        Node cur = head;
        while (cur != null) {
            while (cur.val == key) {
                //考虑头删
                if (cur == head) {
                    head = cur.next;
                    cur = head.next;
                } else {
                    prev.next = cur.next;
                    cur = cur.next;
                }
            }
            prev = cur;
            cur = cur.next;
        }
    }

    //得到单链表的长度
    public int size() {
        int sz = 0;
        Node cur = head;
        while (cur != null) {
            sz++;
            cur = cur.next;
        }
        return sz;
    }

    public void display() {
        Node cur = head;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    public void clear() {
        head = null;
    }
}
